package com.dhcc.controller;

import com.dhcc.api.R;
import com.dhcc.entity.SysUserResource;
import com.dhcc.service.InstitutionRoleDepartmentPositionService;
import com.dhcc.service.SysUserResourceService;
import com.dhcc.utils.ResourceTreeBuilder;
import com.dhcc.vo.SysClientResourceListTree;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userSystem/sys/client/resource")
public class SysClientResourceController {
    @Resource
    private InstitutionRoleDepartmentPositionService institutionRoleDepartmentPositionService;
    @Resource
    private SysUserResourceService sysUserResourceService;
    /**
     * 根据机构编号，机构角色编号，部门id，岗位id查询对应的资源树
     */
    @GetMapping("/getListTree")
    public R<List<SysClientResourceListTree>> getListTree(String institutionId,String institutionRoleId,String departmentId,String positionId){
        List<String> resourceIds=institutionRoleDepartmentPositionService.getResourceIds(institutionId,institutionRoleId,departmentId,positionId);
        List<SysUserResource> resourceList=new ArrayList<>();
        for (String resourceId:resourceIds){
            SysUserResource sysUserResource=sysUserResourceService.getResourceById(resourceId);
            resourceList.add(sysUserResource);
        }
        return R.data(ResourceTreeBuilder.buildResourceTree(resourceList,null));
    }
}
